;
(function (call) {
    var main = {
        userInfo: null,
        mapId: "jcybMap",
        init: function () {
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            var time = new Date().format("yyyy-MM-dd hh:mm");
            this.time = CafeJS.Datepicker({
                appendTo: "#bidTime",
                type: 1,
                dateFmt: "yyyy-MM-dd hh:mm",
                curDate: time
            });
            this.getDialNumber();
        },
        getDialNumber: function() {
            var that = this;
            $.getJSON(api.internal.business.callMonitorCfg, function(res) {
                if (res.isSuccess && res.data.length > 0) {
                    res.data.forEach(function(item) {
                        if (item.org_id+'' == that.userInfo.smuser.orgId) {
                            var $option = $('<li>'+item.phone+'</li>');
                            $option.attr('value', item.dept_name);
                            $('#dialNumberList').append($option);
                        }
                    })
                }
            })
        },
        // 保存事件
        saveEvent: function () {
            var that = this;
            var callTime = $("#bidTime .datepicker span").text();
            var phone = $("#dialNumber").val();
            var callObj = $("#relatedDisasters").val();
            var post = $("#job").val();
            var callResult = $("#isSuccess").val();
            var userName = $("#bidPeople").val();
            if (!userName) {
                 alert("请输入叫应人");
                return;
            }
            var userId = that.userInfo.smuser.iid
            var param = {
                callTime: new Date(callTime).format('yyyy-MM-dd hh:mm:00'),
                phone: phone,
                callObj: callObj,
                post: post,
                callResult: callResult,
                uid: userId,
                userName: userName
            }
            $.ajax({
                url: api.internal.business.callMonitorCreate,
                type: "POST",
                contentType: "application/json;charset=utf-8",
                dataType: 'json',
                data: JSON.stringify(param),
                success: function (res) {
                    if (res.isSuccess) {
                        alert("保存成功");
                        $("#modal_add_bid .close").trigger('click');
                        $("#bidQueryBtn").click();
                    } else {
                        alert("保存失败");
                    }
                }
            })
        },
        bindEvt: function () {
            var that = this;
            $("#sureBtn").click(function () {
                that.saveEvent();
            })
            $("#cancelBtn").click(function () {
                $("#modal_add_bid .close").trigger('click');
            })
            $("#dialNumber").focusin(function(){
                if ($('#dialNumberList li').length > 0)
                    $('#dialNumberList').show();
            }).change(function(){
                $('#dialNumberList').hide();
            })
            $('#dialNumberList').delegate('li', "click", function(){
                $('#dialNumber').val($(this).text());
                $("#relatedDisasters").val($(this).attr('value'));
                $("#dialNumberList").hide();
            })
        },
        //弹出框内容
        alert: function (info) {
            $.dialogMaster({
                "type": 'tip',
                "info": info
            });
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    CafeJS.models(['datepicker'], function () {
        main.init();
    });
});